package com.ws.base.db.support;

import java.util.List;
import java.util.Map;

import com.ws.base.common.util.WSUtil;
import com.ws.base.db.common.order.Order;
import com.ws.base.db.common.page.Page;
import com.ws.base.exception.WSRuntimeException;

public class DBSqlIdSupport {
    public int add(String sqlId, Map<String, Object> condition) {
        String sql = getSqlStr(sqlId);
        if (!WSUtil.isEmpty(sql)) {
            return dbStationsSupport.add(sql, condition);
        } else {
            return -1;
        }
    }

    public int[] addBatch(String sqlId, Map<String, Object>[] conditions) {
        String sql = getSqlStr(sqlId);
        if (!WSUtil.isEmpty(sql)) {
            return dbStationsSupport.addBatch(sql, conditions);
        } else {
            return new int[] {};
        }
    }

    public int delete(String sqlId, Map<String, Object> condition) {
        String sql = getSqlStr(sqlId);
        if (!WSUtil.isEmpty(sql)) {
            return dbStationsSupport.delete(sql, condition);
        } else {
            return -1;
        }
    }

    public int update(String sqlId, Map<String, Object> condition) {
        String sql = getSqlStr(sqlId);
        if (!WSUtil.isEmpty(sql)) {
            return dbStationsSupport.update(sql, condition);
        } else {
            return -1;
        }
    }

    public <T> List<T> queryListForObject(String sqlId,
            Map<String, Object> condition, Page page, Class<T> clazz) {
        String sql = getSqlStr(sqlId);
        if (!WSUtil.isEmpty(sql)) {
            return dbStationsSupport.queryListForObject(sql, condition, page,
                    clazz);
        } else {
            return null;
        }
    }

    public List<Map<String, Object>> query(String sqlId,
            Map<String, Object> condition, Page page, Order order) {
        String sql = getSqlStr(sqlId);
        if (!WSUtil.isEmpty(sql)) {
            return dbStationsSupport.query(sql, condition, page, order);
        } else {
            return null;
        }
    }

    public int count(String sqlId, Map<String, Object> condition) {
        String sql = getSqlStr(sqlId);
        if (!WSUtil.isEmpty(sql)) {
            return dbStationsSupport.count(sql, condition);
        } else {
            return -1;
        }
    }

    private final String getSqlStr(String sqlId) {
        String sql = NamingXmlSupport.getSqlMap().get(sqlId);
        if (WSUtil.isEmpty(sql)) {
            new WSRuntimeException("NOT Fount sqlId, sqlId: " + sqlId);
            return null;
        } else {
            return sql;
        }
    }

    DBStationsSupport dbStationsSupport;

    public DBStationsSupport getDbStationsSupport() {
        return dbStationsSupport;
    }

    public void setDbStationsSupport(DBStationsSupport dbStationsSupport) {
        this.dbStationsSupport = dbStationsSupport;
    }

}
